/**
 * 结算操作按钮管理
 */
var ClientDelegate = require('./../../FrameWork/Delegate/ClientDelegate');
var ClientEveDelegate = require('./../../FrameWork/Delegate/ClientEveDelegate');
var ClientPkDelegateType = require('./../Delegate/ClientPkDelegateType');
var PkSound = require('./../PkSound');
var TimeOutCtr = require('./../../FrameWork/Component/TimeOutCtr');
var EnumPkRoom = require('./../Enum/EnumPkRoom');
var UtilGameObject = require('./../../FrameWork/Utils/UtilGameObject');
var _ = require('./../../FrameWork/we_underscore');
cc.Class({
    extends: ClientDelegate,
    properties: {
        // posShowNodes: [cc.Node], //UI
        timeOutCtr: {
            default: null,
            type: TimeOutCtr,
            displayName: '倒计时组件'
        },
        prefabLb: {
            default: null,
            type: cc.Prefab,
            displayName: '文字预设'
        },
        prefabAnimScore: {
            default: null,
            type: cc.Prefab,
            displayName: '结算分数',
        },
        prefabAnimMoney: {
            default: null,
            type: cc.Prefab,
            displayName: '金币动画',
        },
        nodeReturn: {
            default: null,
            type: cc.Node,
            displayName: '退出'
        },
        nodeChangDesk: {
            default: null,
            type: cc.Node,
            displayName: '换 桌'
        },
        nodeContinue: {
            default: null,
            type: cc.Node,
            displayName: '继 续'
        },
        nodeCell: {
            default: null,
            type: cc.Node,
            displayName: '结算UI'
        }
    },
    iniTest() {
        UtilGameObject.createDemoBtn([
            ['结算分数', function() {
                var endInfo = {
                    seatInfos: [{
                        seatIdx: 0,
                        startNumber: 0,
                        endNumber: 100
                    }, {
                        seatIdx: 1,
                        startNumber: 0,
                        endNumber: -50
                    }, {
                        seatIdx: 2,
                        startNumber: 0,
                        endNumber: -50
                    }, ]
                }
                this.doActiveCell(true);
                this.showResultScore(endInfo);
            }.bind(this)],
        ]);
    },

    onLoad() {
        this._super();
        this.addListerDelegate(ClientPkDelegateType.Clinet_singleEnd, this.doActiveCell.bind(this));
        this.addListerDelegate(ClientPkDelegateType.Clinet_singleEndShowResultUI, this.showResultScore.bind(this));
        var posList = [{
            pos: this.nodeReturn,
            world: '离 开'
        }, {
            pos: this.nodeChangDesk,
            world: '换 桌'
        }, {
            pos: this.nodeContinue,
            world: '继 续'
        }];
        _.each(posList, function(_info) {
            var lbCtr = UtilGameObject.getCtrByPrefabs(this.prefabLb, _info.pos);
            lbCtr.setFontSize(43);
            lbCtr.setPositionY(5);
            lbCtr.setText(_info.world);
            lbCtr.setOutlineWidth2(5);
        }.bind(this));
        this.resetNet();
        this.iniTest();
    },
    setPosShowNodes(_posShowNodes) {
        this.posShowNodes = _posShowNodes;
    },
    setHeadNodes(_headNodes) {
        this.headNodes = _headNodes;
    },
    showResultScore(jData) {
        this.clear();
        let seatInfos = jData.seatInfos;
        let winList = [];
        let failList = [];
        _.each(seatInfos, function(_info) {
            let seatIdx = _info.seatIdx;
            let startNumber = _info.startNumber;
            let endNumber = _info.endNumber;
            let scoreCtr = UtilGameObject.getCtrByPrefabs(this.prefabAnimScore, this.node);
            let worldPos = this.posShowNodes[seatIdx].convertToWorldSpaceAR(cc.v2(0, 0));
            scoreCtr.setPosByWorldPos(worldPos);
            scoreCtr.playRollAnim(startNumber, endNumber);
            if (endNumber > 0) {
                winList.push({
                    seatIdx: seatIdx,
                    money: (endNumber - startNumber),
                });
            } else {
                failList.push({
                    seatIdx: seatIdx,
                    money: (startNumber - endNumber),
                });
            }
            this.scoreCtrList.push(scoreCtr);
        }.bind(this));

        _.each(failList, function(failInfo) {
            let failMoney = failInfo.money;
            /**找赢家 */
            for (let i = 0; i < _.size(winList); ++i) {
                let winNeedMoney = winList[i].money;
                /***赢得比较多 */
                if (winNeedMoney > 0 && failMoney <= winNeedMoney) {
                    winList[i].money -= failMoney;
                    failInfo.money -= failMoney;
                    this.playEndMoneyAnim(failInfo.seatIdx, winList[i].seatIdx, failMoney);
                } else {
                    let _money = winList[i].money;
                    failInfo.money -= _money;
                    winList[i].money -= 0;
                    this.playEndMoneyAnim(failInfo.seatIdx, winList[i].seatIdx, _money);
                    continue; //这个玩家只赢了一部分 所以要接着找其他的赢家
                }
            }
        }.bind(this))
    },
    playEndMoneyAnim(_failSeatIdx, winSeatIdx, _money) {
        let cnt = _.random(10, 20);
        for (let i = 0; i < cnt; ++i) {
            let moneyCtr = UtilGameObject.getCtrByPrefabs(this.prefabAnimMoney, this.node);
            let worldPosStart = this.headNodes[_failSeatIdx].convertToWorldSpaceAR(cc.v2(0, 0));
            let worldPosEnd = this.headNodes[winSeatIdx].convertToWorldSpaceAR(cc.v2(0, 0));
            let posStart = moneyCtr.getPositionByWorld(worldPosStart);
            let posEnd = moneyCtr.getPositionByWorld(worldPosEnd);
            let randArea = 80;
            let posMidd = cc.v2(_.random(-randArea, randArea), _.random(-randArea, randArea) + 50)
            moneyCtr.setPosition(posStart);
            moneyCtr.setOpacity(0);
            moneyCtr.playAnimDelay((1.5 + _.random(1, 20) * 0.01), function() {
                if (0 == i) {
                    PkSound.playMoneyAnim();
                    ClientEveDelegate.doDataToLister(ClientPkDelegateType.Client_headAddMoney, {
                        seatIdx: _failSeatIdx,
                        money: -_money
                    });
                }
            }, posMidd, posEnd, function() {
                if (0 == i) {
                    PkSound.playMoneyAnim();
                    ClientEveDelegate.doDataToLister(ClientPkDelegateType.Client_headAddMoney, {
                        seatIdx: winSeatIdx,
                        money: _money
                    });
                }
            });
        }
    },
    clear() {
        if (null != this.scoreCtrList) {
            _.each(this.scoreCtrList, function(ctr) {
                ctr.node.destroy()
            });
        }
        this.scoreCtrList = [];
    },
    resetNet() {
        var isGameEnd = (window.g_room && EnumPkRoom.GAMINGEND == window.g_room.getRoomState());
        this.doActiveCell(isGameEnd);
    },
    doActiveCell(_is) {
        this.nodeCell.active = _is;
        if (!!_is) {
            this.openTimeOut();
        } else {
            this.timeOutCtr.doClose();
        }
    },
    /**换桌 */
    eveChangDesk() {
        if (window.g_room) {
            window.g_room.doChangDesk();
        }
        this.clear();
        this.doActiveCell(false);
    },

    /**离 开*/
    eveReturn() {
        this.clear();
        MgScene.Inst().gotoSceneByName('HallPortrait');
    },

    /**继续 */
    eveContinue() {
        if (window.g_room) {
            window.g_room.doContinue();
        }
        this.clear();
        this.doActiveCell(false);
    },
    /**开启倒计时 */
    openTimeOut(_timeOut) {
        _timeOut = null == _timeOut ? 10 : _timeOut;
        this.timeOutCtr.startCountDown(_timeOut); //this.eveChangDesk.bind(this)
    }
});